﻿#region

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Gma.System.MouseKeyHook;

#endregion

namespace 五分钟倒计时
{
    public partial class Form1 : Form
    {
        private IKeyboardMouseEvents m_GlobalHook;
        [DllImport("user32.dll")]
        public static extern IntPtr GetForegroundWindow();
        public Form1()
        {
            InitializeComponent();
            Subscribe();
        }

        public void Subscribe()
        {
            // Note: for the application hook, use the Hook.AppEvents() instead
            m_GlobalHook = Hook.GlobalEvents();

            m_GlobalHook.MouseDownExt += GlobalHookMouseDownExt;
            m_GlobalHook.KeyPress += GlobalHookKeyPress;
        }

        private void GlobalHookKeyPress(object sender, KeyPressEventArgs e)
        {
            Console.WriteLine("KeyPress: \t{0}", e.KeyChar);
        }

        private void GlobalHookMouseDownExt(object sender, MouseEventExtArgs e)
        {
            Console.WriteLine("MouseDown: \t{0}; \t System Timestamp: \t{1}", e.Button, e.Timestamp);

            // uncommenting the following line will suppress the middle mouse button click
            // if (e.Buttons == MouseButtons.Middle) { e.Handled = true; }
        }

        public void Unsubscribe()
        {
            m_GlobalHook.MouseDownExt -= GlobalHookMouseDownExt;
            m_GlobalHook.KeyPress -= GlobalHookKeyPress;

            //It is recommened to dispose it
            m_GlobalHook.Dispose();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Unsubscribe();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.timer1.Enabled = true;
            this.button2.BackColor = Color.Black;
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            this.timer1.Enabled = false;
            ColorDialog colorDialog = new ColorDialog();
            if (colorDialog.ShowDialog() != DialogResult.OK)
            {
                this.timer1.Enabled = true;
                return;
            }
            if (colorDialog.Color.ToArgb() == -65537)
            {
                MessageBox.Show("与透明色重复请选择其他颜色！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                this.timer1.Enabled = true;
                return;
            }
            this.button2.BackColor = colorDialog.Color;
            this.timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (GetForegroundWindow() == base.Handle)
            {
                return;
            }
            base.TopMost = true;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            base.TopMost = false;
            int 倒计时;
            int 字体大小;
            try
            {
                倒计时 = int.Parse(this.textBox1.Text);
                字体大小 = int.Parse(this.textBox2.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("请输入正确数字！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                this.timer1.Enabled = true;
                return;
            }
            Settings settings = new Settings();
            settings.倒计时 = 倒计时;
            settings.位置 = base.Location;
            settings.字体大小 = 字体大小;
            settings.颜色 = this.button2.BackColor;
            this.timer1.Enabled = false;
            base.Hide();
            new Form2(settings).ShowDialog();
            base.Close();
        }
    }
    [Serializable]
    public class Settings
    {
        public int 倒计时 { get; set; }
        public int 字体大小 { get; set; }
        public Point 位置 { get; set; }
        public Color 颜色 { get; set; }
    }
}